package com.ofek.eventos;

import java.io.Serializable;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.support.v4.content.LocalBroadcastManager;

public class EventosAndroidUtils {

	public static void startFormActivity(final Context ctx,final String action,final Serializable contextDefiner) {
		final Intent intent = new Intent(ctx, FormActivity.class);
		intent.putExtra(FormActivity.INTENT_KEY_EXTRA_ACTION, action);
		if (contextDefiner!=null) intent.putExtra(FormActivity.INTENT_KEY_EXTRA_DATA,contextDefiner);
		ctx.startActivity(intent);
	}

	/**
	 * @param eventosActivity
	 * @param actionFormContext
	 */
	public static void startFormActivity(final Context ctx,final String action) {
		startFormActivity(ctx,action,null);
	}

	/**
	 * 
	 */
	public static void recreateActivityOnEventBroadcast(final Activity a, final IntentFilter event) {
		LocalBroadcastManager.getInstance(a).registerReceiver(new BroadcastReceiver() {

			@Override
			public void onReceive(final Context context, final Intent intent) {
				if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
					a.recreate();
				}
				else {
					a.finish();
					a.startActivity(a.getIntent());
				}
				LocalBroadcastManager.getInstance(a).unregisterReceiver(this);
			}

		}, event);

	}

	/**
	 * @param a
	 * @param eventNotifyDataSetChanged
	 */
	public static void recreateActivityOnEventBroadcast(final PageActivity a,final String event) {
		recreateActivityOnEventBroadcast(a,new IntentFilter(event));
	}

}
